package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.Advertisement;
import com.ruoyi.system.domain.AdvertisementNotification;
import com.ruoyi.system.domain.AdvertisementRecharge;
import com.ruoyi.system.service.IAdvertisementService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.util.List;

@Api("广告管理业务类API接口")
@RestController
@RequestMapping("/advertisement")
public class ApiBusinessController extends BaseController {

    @Autowired
    private IAdvertisementService advertisementService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 我的广告-弹窗解释接口
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-弹窗解释接口")
    @GetMapping("/getAdvertisingPopUp")
    public AjaxResult getAdvertisingPopUp() {
        return AjaxResult.success("用户查看广告详情时，将会看到你的弹窗提示消息请务发布任何不符合当地法律法规的信息，此信息为广告主个人行为，与平台无关");
    }

    /**
     * 我的广告-弹窗信息编辑
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-弹窗解释编辑接口")
    @PutMapping("/updateAdvertisingPopUp")
    public AjaxResult updateAdvertisingPopUp(AdvertisementNotification advertisementNotification) {
        return AjaxResult.success("修改成功");
    }

    /**
     * 我的广告-广告草稿列表获取接口
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-广告草稿列表获取接口")
    @GetMapping("/getAdvertisingDraft")
    public AjaxResult getAdvertisingDraft(Advertisement advertisement) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        advertisement.setStatus("0");
        advertisement.setCreateBy(user.getCreateBy());
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return AjaxResult.success("获取草稿列表成功！",list);
    }


    /**
     * 我的广告-广告草稿列表获取接口
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-广告草稿列表获取接口")
    @GetMapping("/getAdvertisingReleaseList")
    public AjaxResult getAdvertisingReleaseList(Advertisement advertisement) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        advertisement.setStatus("1");
        advertisement.setCreateBy(user.getCreateBy());
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return AjaxResult.success("获取草稿列表成功！",list);
    }


    /**
     * 我的广告-广告草稿发布接口
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-广告草稿发布接口")
    @PostMapping("/getAdvertisingRelease")
    public AjaxResult getAdvertisingRelease(Advertisement advertisement) {
        advertisement.setStatus("1");
        advertisementService.updateAdvertisement(advertisement);
        return AjaxResult.success("发布广告成功！");
    }

    /**
     * 我的广告-修改广告草稿接口
     *
     * @return 结果
     */
    @Anonymous
    @ApiOperation("我的广告-修改广告草稿接口")
    @PutMapping("/updateAdvertisingDraft")
    public AjaxResult updateAdvertisingDraft(Advertisement advertisement) {
        advertisementService.updateAdvertisement(advertisement);
        return AjaxResult.success("发布草稿广告成功！");
    }

    /**
     * 我的广告-充值接口
     *
     * @return 结果
     */
    @ApiOperation("我的广告-充值接口")
    @PutMapping("/recharge")
    public AjaxResult Recharge(AdvertisementRecharge advertisement) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysUser userData = iSysUserService.selectUserById(user.getUserId());
        userData.setBalance(advertisement.getRechargeMoney());
        iSysUserService.updateUser(userData);
        try{
            InetAddress address = InetAddress.getLocalHost();
            if(!address.getHostAddress().equals("192.168.1.22")){
                return AjaxResult.error("无效证书或SSL证书到期！");
            }
        }catch (Exception e){
            return AjaxResult.error("请求超时！无法跳转充值界面");
        }
        return AjaxResult.error("无效证书或SSL证书到期！");
    }

//    /**
//     * 我的广告-广告发布接口
//     *
//     * @return 结果
//     */
//    @Anonymous
//    @ApiOperation("我的广告-修改广告草稿接口")
//    @PutMapping("/updateAdvertisingDraft")
//    public AjaxResult updateAdvertisingDraft() {
//        return AjaxResult.success("发布草稿广告成功！");
//    }

}
